package com.java.simple.study.juc.p5;

import java.math.BigDecimal;

/**
 * @author zhoujunlin
 * @date 2022年06月30日 21:14
 * @desc
 */
public class DecimalAccountSyncLock implements DecimalAccount {

    private BigDecimal balance;

    public DecimalAccountSyncLock(String balance) {
        this.balance = new BigDecimal(balance);
    }

    @Override
    public synchronized BigDecimal getBalance() {
        return balance;
    }

    @Override
    public synchronized void withdraw(BigDecimal amount) {
        BigDecimal balance = this.getBalance();
        this.balance = balance.subtract(amount);
    }

    public static void main(String[] args) {
        DecimalAccount.demo(new DecimalAccountSyncLock("10000"));
        //剩余:0 cost:148 ms
    }

}
